package com.app.server;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import com.app.task.Task;

public class Broadcast extends Task {
	
	private DatagramSocket socket;
	private List<DatagramPacket> packets;
        private List<InetAddress> broadcastList;
	private String gameName = "unnamed game";
        private InetAddress submask; 
	private int port;
        
	public Broadcast(int port){
            this.port = port;
            try{
                submask = InetAddress.getByName("255.255.255.0");
			socket = new DatagramSocket();
			socket.setBroadcast(true);
		}catch(Exception e){
			
		}
            brodcastHostAddress();
	}
	
	@Override
	protected void task() {
		
		sendList();
	try{
		Thread.sleep(200);
	}catch(Exception e){
		e.printStackTrace();
	}

	}
	private void brodcastHostAddress(){
		packets = new ArrayList<DatagramPacket>();
                broadcastList = new ArrayList<InetAddress>();
		try{
			Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
			while(ifaces.hasMoreElements()){
				NetworkInterface network = ifaces.nextElement();
                                
				List<InterfaceAddress> iaddr = network.getInterfaceAddresses();
                                
				for(InterfaceAddress addr: iaddr){
                                    if(addr!=null){
					InetAddress broadcastIP = addr.getBroadcast();
                                        if(addr.getNetworkPrefixLength()==8 || addr.getNetworkPrefixLength()==16|| addr.getNetworkPrefixLength()==24){
					if(broadcastIP!=null && broadcastIP.getAddress()[0] != 127 &&  broadcastIP.getAddress()[0] != 0){
						broadcastList.add(broadcastIP);
						System.out.println(broadcastIP);
					}
                                        }else if(broadcastIP!=null){
                                            byte[] broadcast = new byte[4];
                                            byte[] mask = submask.getAddress();
                                            byte[] adr = addr.getAddress().getAddress();
                                            for(int i=0; i<4;++i){
                                                broadcast[i] = (byte) (adr[i] | ~mask[i]);
                                            }
                                            InetAddress broadAddress = InetAddress.getByAddress(broadcast);
                                            broadcastList.add(broadAddress);
                                            System.out.println("BORAD === " + broadAddress);
                                        }		
				}
				
			}
                        }
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	private void sendList() {
		try {
			
			for(InetAddress br : broadcastList){
                            //System.out.println(dp.getAddress());
                            String data = gameName;
                            DatagramPacket dp = new DatagramPacket(data.getBytes(), data.length(),br,port);
                            socket.send(dp);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public String getGameName() {
		return gameName;
	}
	public void setGameName(String gameName) {
		this.gameName = gameName;
	}
}
